package org.chnxi.study.datastructrues.doublelinkedlist;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class DoubleLinkedLisTest {

    public static void main(String[] args) {
        System.out.println("双向链表的测试");
        Person p1 = new Person(1,"AAA" , "aaa");
        Person p2 = new Person(2,"BBB" , "bbb");
        Person p3 = new Person(3,"CCC" , "ccc");
        Person p4 = new Person(4,"DDD" , "ddd");

        System.out.println("向双向链表中增加节点");
        DoubleLinkedLis<Person> list = new DoubleLinkedLis<Person>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(null);
        list.add(p4);

        System.out.println("输出双向链表中的数据");
        list.printList();

        System.out.println("修改双向链表中的数据");
        Person p3New = new Person(3,"ccc" , "CCC");
        Person p4New = new Person(4,"ddd" , "DDD");
        list.update(p3New);
        list.update(p4New);

        System.out.println("输出修改后的双向链表中数据");
        list.printList();

        System.out.println("删除双向链表中的数据");
        Person p3del = new Person(3 , "ccc" , "CCC");
        list.delete(p3del);
        System.out.println("删除后双向链表中的数据");
        list.printList();

        System.out.println("按照位置idx向双向链表中添加数据");
        Person p5 = new Person(5 , "EEE" , "eee");
        Person p6 = new Person(6 , "FFF" , "fff");
        Person p7 = new Person(7 , "GGG" , "ggg");

        list.add(0 , p5);
        list.add(5 , p6);
        list.add(1 , p7);

        System.out.println("输出添加数据后的双向链表");
        list.printList();


    }

}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private Integer idx;
    private String name;
    private String nickName;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return idx.equals(person.idx);
    }

    @Override
    public int hashCode() {
        return Objects.hash(idx);
    }
    //    @Override
//    public String toString() {
//        return "Person{" +
//                "idx=" + idx +
//                ", name='" + name + '\'' +
//                ", nickName='" + nickName + '\'' +
//                '}';
//    }
}
